/// 用户模型
class UserModel {
  final String id;
  final String username;
  final String tel;
  final String headUrl;
  final String address;
  final String shopName;
  final String roleType;
  final String vipLevel;
  final bool isVip;
  final String endTime;
  final bool isEmployee;
  final Map<String, bool> permissions;

  const UserModel({
    required this.id,
    required this.username,
    required this.tel,
    required this.headUrl,
    required this.address,
    required this.shopName,
    required this.roleType,
    required this.vipLevel,
    required this.isVip,
    required this.endTime,
    required this.isEmployee,
    required this.permissions,
  });

  /// 从JSON创建用户模型
  factory UserModel.fromJson(Map<String, dynamic> json) {
    return UserModel(
      id: json['_id'] ?? json['id'] ?? '',
      username: json['username'] ?? '',
      tel: json['tel'] ?? '',
      headUrl: json['headurl'] ?? json['headUrl'] ?? '',
      address: json['address'] ?? '',
      shopName: json['shopname'] ?? json['shopName'] ?? '',
      roleType: json['roletype'] ?? json['roleType'] ?? '2',
      vipLevel: json['viplevel'] ?? json['vipLevel'] ?? '0',
      isVip: (json['isVip'] ?? json['vip'] ?? '0') == '1',
      endTime: json['endTime'] ?? '',
      isEmployee: (json['roletype'] ?? '2') != '2',
      permissions: {
        'canAddContact': (json['iscanaddnewcontact'] ?? '0') == '1',
        'canDeleteContact': (json['iscandelcontact'] ?? '0') == '1',
        'canEditContact': (json['iscaneditcontact'] ?? '0') == '1',
        'canKaiDan': (json['iscankaidan'] ?? '0') == '1',
        'canSeeContactRepairs': (json['iscanseecontactrepairs'] ?? '0') == '1',
        'isDirectAddItem': (json['isdirectadditem'] ?? '0') == '1',
        'isNeedDispatch': (json['isneeddispatch'] ?? '0') == '1',
      },
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'username': username,
      'tel': tel,
      'headUrl': headUrl,
      'address': address,
      'shopName': shopName,
      'roleType': roleType,
      'vipLevel': vipLevel,
      'isVip': isVip,
      'endTime': endTime,
      'isEmployee': isEmployee,
      'permissions': permissions,
    };
  }

  /// 复制并更新字段
  UserModel copyWith({
    String? id,
    String? username,
    String? tel,
    String? headUrl,
    String? address,
    String? shopName,
    String? roleType,
    String? vipLevel,
    bool? isVip,
    String? endTime,
    bool? isEmployee,
    Map<String, bool>? permissions,
  }) {
    return UserModel(
      id: id ?? this.id,
      username: username ?? this.username,
      tel: tel ?? this.tel,
      headUrl: headUrl ?? this.headUrl,
      address: address ?? this.address,
      shopName: shopName ?? this.shopName,
      roleType: roleType ?? this.roleType,
      vipLevel: vipLevel ?? this.vipLevel,
      isVip: isVip ?? this.isVip,
      endTime: endTime ?? this.endTime,
      isEmployee: isEmployee ?? this.isEmployee,
      permissions: permissions ?? this.permissions,
    );
  }

  @override
  String toString() {
    return 'UserModel(id: $id, username: $username, tel: $tel, isEmployee: $isEmployee)';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is UserModel && other.id == id;
  }

  @override
  int get hashCode => id.hashCode;
}
